#pragma once
#include"server.hpp"
class EchoServer{
private:
    TcpServer _server;
private:
    void OnMessage(const PtrConnection &conn, Buffer *buf){
        lg(Info,"%s",buf->ReadPosition());
        buf -> MoveReadOffset(buf->ReadAbleSize());
        std::string str = "server hello World";
        conn->Send(str.c_str(),str.size());
        // conn->Shutdown();//调用关闭Connection
    }

    void OnClosed(const PtrConnection &conn){
        DBG_LOG("CLOSE CONNECTION:%p",conn.get());//conn.get()得到指针地址
    }

    void OnConnected(const PtrConnection &conn){
        lg(Info,"Connection Established:%p",conn.get());
        // DBG_LOG("查看线程地址");
    }

public:
    EchoServer(int port):_server(port){
    }
    void start(){
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
        _server.SetCounnectedCallback(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
        _server.Start();
    }
};